package homeWorkCode;


import node.SeqNode;

public class BookDemo<T> {

    SeqNode<T> head = new SeqNode<>();

    SeqNode<T> tail = this.head;

    void add(SeqNode<T> data) {
        if (null != data) {
            this.tail.setNext(data);
            this.tail = tail.getNext();
        }
    }

    void show() {
        SeqNode<T> current = this.head.getNext();
        while (current != null) {
            System.out.print(current.getData() + " ");
            current = current.getNext();
        }
        System.out.println();
    }

    // 时间复杂度 O(n)
    void delete(T k) {
        SeqNode<T> current = this.head;
        // 判断为null 的条件
        while (current != null && current.getNext() != null && current.getNext().getNext() != null) {
            // 判断k是否是第一个节点
            if (this.head.getNext().getData().equals(k)) {
                current = current.getNext();
                continue;
            }
            // 判断下下一节点是否是k
            if (current.getNext().getNext().getData().equals(k)) {
                current.setNext(current.getNext().getNext());
                return;
            }
            current = current.getNext();
        }
    }


    public static void main(String[] args) {
        // 测试链表
        BookDemo<Integer> bookDemo = new BookDemo<>();
        for (int i = 1; i < 11; i++) {
            bookDemo.add(new SeqNode<>(i));
        }


        bookDemo.show();
        bookDemo.delete(1);
        bookDemo.show();
    }
}
